<?php

class Prototype_Tab_Widget extends WP_Widget {

	var $defaults = array(
		'sidebar' => '',
		'class' => ''
	);

	function __construct( $id_base = false, $name = '', $widget_options = array(), $control_options = array() ) {

        parent::__construct(
			'prototype-tabs',
			__( 'Prototype Tabs',  TEXTDOMAIN ),
			array( 'description' => __( 'Sidebar into tabbed widget', TEXTDOMAIN ) )
		);

    }

    public function widget($args, $instance) {
        add_filter('dynamic_sidebar_params', array(&$this, 'widget_sidebar_params'));

        extract($args, EXTR_SKIP);

        echo $before_widget;
            if ($args['id'] != $instance['sidebar']) {
                echo '<div id="'.$widget_id.'" class="d4p-tabber-widget">';
                    echo '<ul class="d4p-tabber-header"></ul>';
                    dynamic_sidebar($instance["sidebar"]);
                echo '</div>';
            } else {
                echo 'Tabber widget is not properly configured.';
            }
        echo $after_widget;

        remove_filter('dynamic_sidebar_params', array(&$this, 'widget_sidebar_params'));
    }

    public function widget_sidebar_params($params) {
        $params[0]['before_widget'] = '<div id="'.$params[0]['widget_id'].'" class="d4p-st-tab">';
        $params[0]['after_widget'] = '</div>';
        $params[0]['before_title'] = '<a href="#" class="d4p-st-title">';
        $params[0]['after_title'] = '</a>';

        return $params;
    }

    public function form($instance) {
        global $wp_registered_sidebars;

        $instance = wp_parse_args((array)$instance, $this->defaults);

        ?>

        <p><label>Sidebar:</label>
            <select style="background-color: white;" class="widefat d4p-tabber-sidebars" id="<?php echo $this->get_field_id('sidebar'); ?>" name="<?php echo $this->get_field_name('sidebar'); ?>">
                <?php

                foreach ($wp_registered_sidebars as $id => $sidebar) {
                    if ($id != 'wp_inactive_widgets') {
                        $selected = $instance['sidebar'] == $id ? ' selected="selected"' : '';
                        echo sprintf('<option value="%s"%s>%s</option>', $id, $selected, $sidebar['name']);
                    }
                }

                ?>
            </select><br/>
            <em>Make sure not to select Sidebar holding this widget. If you do that, Tabber will not be visible.</em>
        </p>
        <p><label>CSS Class:</label>
            <input class="widefat" id="<?php echo $this->get_field_id('class'); ?>" name="<?php echo $this->get_field_name('class'); ?>" type="text" value="<?php echo $instance['class']; ?>" />
        </p>

        <?php
    }

    public function update($new_instance, $old_instance) {
        $instance = $old_instance;

        $instance['sidebar'] = strip_tags(stripslashes($new_instance['sidebar']));
        $instance['class'] = strip_tags(stripslashes($new_instance['class']));

        return $instance;
    }



}

/*
 **************************************************************************************************
 *	Register tabs sidebar
 **************************************************************************************************
*/
function prototype_tabs_sidebar() {

    register_sidebar( array(
		'name' => __( 'Tabs Sidebar', TEXTDOMAIN ),
		'description' => __( 'Add widgets to this sidebar to use it from tabs widget', TEXTDOMAIN )
	) );

}
// runs after wordpress has finished loading but before any headers are sent
add_action( 'init', 'prototype_tabs_sidebar' );

/*
 **************************************************************************************************
 *	Load custom widget
 **************************************************************************************************
*/
function prototype_tabs_widget() {

	// register our widget in wordpress
	register_widget( 'Prototype_Tab_Widget' );

}
// widget can then be registered
add_action( 'widgets_init', 'prototype_tabs_widget' );

?>